package cn.topevery.garden.dal.logic.warning;

import cn.topevery.garden.dal.logic.base.WarningBase;
import cn.topevery.garden.entity.po.IotDeviceToiletsLog;
import cn.topevery.garden.framework.util.WarningConfigUtil;
import hw.topevery.basis.entity.po.SysClass;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：peng.tian
 * @date ：Created in 2021/9/26 11:37
 * @description：公厕预警处理
 */
@Component
public class IotDeviceToiletsWarning extends WarningBase {
    @Override
    public String getClassId() {
        return "37F28D81-F37C-455E-BB21-054EEAF696DB";
    }

    @Override
    public String getFacilitiesType() {
        return "4ccc6c9a-80ae-4c03-be6d-23b22e13bb09";
    }

    @Override
    public String getWarningType() {
        return "89A7D7FA-9C39-4E94-8529-87A63CB7AA68";
    }


    public void check(IotDeviceToiletsLog data) {
        try {
            List<SysClass> sysClassList = this.getWarningItems();
            Map filedsInfo = this.getFiledsInfo(data);
            StringBuilder warningDesc = new StringBuilder();
            if (sysClassList != null && sysClassList.size() > 0) {
                sysClassList.forEach(f -> {

                    Map<String, String> columnMap = new HashMap<>();
                    if (f.code.contains("/")) {
                        String[] keyVals = f.code.split("/");
                        for (int i = 0; i < keyVals.length; i++) {
                            String[] keyval = keyVals[i].split(":");
                            columnMap.put(keyval[1] + "厕" + f.name, keyval[0]);
                        }
                    } else {
                        columnMap.put(f.name, f.code);
                    }

                    Double threshold = this.getValue(f.classId);
                    columnMap.keySet().forEach(name -> {
                        String code=columnMap.get(name);
                        if (filedsInfo.containsKey(code)) {
                            Double value = Double.parseDouble(filedsInfo.get(code).toString());
                            try {
                                if(WarningConfigUtil.Judge(value,threshold,f.remark)) {
                                    warningDesc.append(name + ",当前值:" + value.toString() + ",阈值:" + threshold.toString() + "\n");
                                    this.setSmallClassId(f.classId);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });

                });
                if (warningDesc.toString().length() > 0) {
                    this.sendWarning(data.getDeviceCode(), warningDesc.toString(), data.getId());
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("公厕预警检测异常!");
        }
    }
}
